package com.edooteach.api.web.controller.book;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.edoomath.entity.course.UserCourse;
import com.edooteach.api.servcice.UserCourseService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/user-products")
public class UserCourseApiController {
    
    @Autowired
    private UserCourseService userCourseService;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult list(@RequestParam("unionid") String unionid) {
      
      HttpResult httpResult = new HttpResult();
      
      List<UserCourse> userCoursesList = userCourseService.listAll(unionid);
      Map<String, List<UserCourse>> userCourses = new HashMap<String, List<UserCourse>>();
      
      if(userCoursesList.isEmpty()) {
          return httpResult.success()
                  .data("userCourses", null)
                  .data("userCoursesSize", 0)
                  .message("获取所有书籍成功");
      } else {
          userCourses = CollectionUtils.constructMapValues(userCoursesList, "course.id");
          
          return httpResult.success()
                           .data("userCourses", userCourses)
                           .data("userCoursesSize", userCourses.size())
                           .message("获取所有书籍成功");
      }
      
    }
    
    /**
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult detail(@PathVariable("id") Long id, @RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        UserCourse userCourse = userCourseService.load(id, unionid);
        
        return httpResult.success()
                         .data("userCourse", userCourse)
                         .message("获取所有书籍成功");
    }

}
